home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-06-08 | 5.5 KB | 113 lines | [TEXT/DEDL] |
- •••••••••••••••••••••••••••••
- SCANNER, PARSER AND EVALUATOR
- •••••••••••••••••••••••••••••
-
- INTRO:
- ------
-
- This program can read, analyse and evaluate any mathematical expression that's given in so
- called Infix notation. This is no normal way of writing math expressions. You can supply it
- with a value for the variable x and the expression will be evaluated correctly.
-
- This is very useful, if you are programming a plot program and want to be able to let the
- program evaluate the curve plots through functions that are not pre defined in the program
- code but can be established during the runtime of the program. There are also many other
- examples where such function parsers become very useful. When you compile any program with
- your BASIC compiler, it does just the same sort of scanning expressions and parsing them.
-
- Look at the programming code to see how this is done in simple ZBASIC or FUTURE BASIC. I
- have used an intrinsic recursive method. Intrinsic, because you have to examine the code a
- little while, before you see the recursion.
-
- You can use this code free and you can change it in any way you like, but please post me a
- message and your own code on Compuserve, if you made your own useful changes to this code.
-
- One variable x can be used. All operators +, -, *, /, ^ and many math functions are supported.
- If not implemented logical functions yet, allthough it's possible to do that. Perhaps you
- would like to add this type of functions.
-
- You can write in small, in capital letter or both mixed up. All three types of brackets can
- be used. If you write π or e the program will change it to the appropriate values of pi and
- the euler constant. You may use any number spaces, they will be skipped, when the program
- parses the expression.
-
- What makes this parser a little bit special (beside the unique way of implementing it in
- BASIC) is the fact, that you can ommit the multiplication sign "*" in allmost any cases.
- As a rule: You can always ommit "*" when normal mathematical convention also allows it.
-
- a) between value (left) and x: 2x is 2*x,
- b) between x and x: xx is x*x,
- c) between value (left) and bracket: 2(x-4) is 2*(x-4),
- d) between x and bracket (both sides): x(3-x) or (3-x)x is x*(3-x) or (3-x)*x
- e) between bracket and bracket: (x-2)(4+x) is (x-2)*(4+x)
- f) between value (left) and funktion: 4sin(x) is 4*sin(x)
- g) between x and function (both sides): xexp(x) or exp(x)x is x*exp(x) or exp(x)*x
- h) between bracket (left) and function: (x-1)cos(x) is (x-1)*cos(x)
- i) between functions: sin(x)ln(x) is sin(x)*ln(x)
- Negative Exponenten don't need brackets: x^-3 is x^(-3)
-
- In the following cases you must use the "*" operator:
-
- a) between brackets(left) and values: (x-4)*3
- b) between values: 3*4
- c) between x (left) and values: x*2
- d) between functions (left) and values: tan(x)*5
-
- Error routines are implemented, but only very rough. So it's your task to add your special
- error trpping code.
-
-
- I hope this is useful for you. It's my first greater attempt in parsing technics and it
- took me a while to get it work like it should.
- So, please give me some response if you like it or if your have any further suggestions,
- questions. I'm also very pleased, if you would inform me about any possible bug in this
- program.
-
-
- COMPILING:
- ----------
- First start ZBASIC or FUTURE BASIC with this code and make shure, that you have changed
- the preferences to the following values:
-
- Default Variable Type: Integer
- Convert to Upper Case: Yes
- Space Req.After Key Words: Yes
- Array Base 1: Yes
- Type: APPL
- Creator: TERM
-
- It's essential to use this program identifier together with the resource file "Terme.Res".
- Look for the line with: Res%=FN OPENRESFILE... and deactivate it.
- After that simply compile this program. After that load "Terme.Res" into ResEdit and also
- open the new program "Terme". Then delete all the "DITL" and "DLOG" resources from the
- program, because you don't need them here and paste all the resources from "Terme.Res" to
- the program. That's it!
-
- You can now start "Terme" and try some math expressions. Please notice, that you don't need
- to write it in the form "y=x+4....", simply write down the right hand side of this function.
- If you don't give in any value for the variable x then zero is choosen automatically.
-
- If you give in invalid characters like "!" or "#", the program will give you an error
- warning and skip immediately skip that character. You may use "." or "," as comma, because
- it will instantaniously be converted to "." anyway. You can easily implement more error
- sophisticated trapping while writing expressions or values.
-
- After some milliseconds you get the value of the function for this x value.
-
-
- END:
- ----
- If you want to contact me, here is my address and user id#:
-
- Detlef Reimers
- Germany
- 22457 Hamburg
- Süntelstraße 7
- Tel: 049-40-559-2773
- Compuserve#: 10015,1146
-
- Thank you for your interest and good look!
-
- 4. June 1994
- Detlef Reimers
-